package com.softtek.gui;

import java.awt.BorderLayout;
import java.util.Iterator;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionListener;

import com.softtek.history.History;

/**
 * Shows a JList with the current history
 * @author Chepech
 *
 */
public class HistoryPanel extends JPanel{
	
	private static final long serialVersionUID = -987262655866349008L;
	private JList historyList;
	private DefaultListModel historyListModel;
	
	public HistoryPanel(final ListSelectionListener listener, final History history){
		super(new BorderLayout());
		
		historyListModel = new DefaultListModel();
		
		for(Iterator<String> historyItem = history.getHistoryList().iterator(); historyItem.hasNext(); ){
			historyListModel.addElement(historyItem.next());
		}
		
		historyList = new JList( historyListModel );
		historyList.addListSelectionListener(listener);
		
		add(historyList, BorderLayout.CENTER);
		
		
	}
	/**
	 * Updates the history list to the current one
	 * @param history - current history handler
	 */
	public void refresh( final History history ){
		
		historyListModel.clear();
		
		for(Iterator<String> historyItem = history.getHistoryList().iterator(); historyItem.hasNext(); ){
			String historyUrl = historyItem.next();
			historyListModel.addElement(historyUrl);
		}
		
		historyList.setModel(historyListModel);
	}
}
